home *** CD-ROM | disk | FTP | other *** search
/ Champak 132 (Alt) / Vol 132.iso / games / rong.swf / scripts / %3Cdefault package%3E / CPaddle.as < prev    next >
Encoding:
Text File  |  2011-06-09  |  10.7 KB  |  327 lines

  1. function FindLineIntersections(rayOrigin, rayDirection, circleCenter, circleRadius)
  2. {
  3.    var resultPoints = new Array();
  4.    var kDiff = new Object();
  5.    kDiff.x = rayOrigin.x - circleCenter.x;
  6.    kDiff.y = rayOrigin.y - circleCenter.y;
  7.    var fA0 = kDiff.x * kDiff.x + kDiff.y * kDiff.y - circleRadius * circleRadius;
  8.    var fA1 = rayDirection.x * kDiff.x + rayDirection.y * kDiff.y;
  9.    var fA2 = rayDirection.x * rayDirection.x + rayDirection.y * rayDirection.y;
  10.    var fDiscr = fA1 * fA1 - fA0 * fA2;
  11.    if(fDiscr > 0)
  12.    {
  13.       var fInvA2 = 1 / fA2;
  14.       fDiscr = Math.sqrt(fDiscr);
  15.       resultPoints[0] = (- fA1 - fDiscr) * fInvA2;
  16.       resultPoints[1] = (- fA1 + fDiscr) * fInvA2;
  17.    }
  18.    else if(fDiscr == 0)
  19.    {
  20.       resultPoints[0] = (- fA1) / fA2;
  21.    }
  22.    return resultPoints;
  23. }
  24. function FindRayCircleIntersections(rkRay, rkCircle)
  25. {
  26.    var resultIntersections = FindLineIntersections(rkRay.origin,rkRay.direction,rkCircle.center,rkCircle.radius);
  27.    var intersectionPoints = new Array();
  28.    if(resultIntersections.length)
  29.    {
  30.       if(resultIntersections.length == 1)
  31.       {
  32.          if(resultIntersections[0] < 0)
  33.          {
  34.             resultIntersections.splice(0);
  35.          }
  36.       }
  37.       else if(resultIntersections[1] < 0)
  38.       {
  39.          resultIntersections.splice(0);
  40.       }
  41.       else if(resultIntersections[0] < 0)
  42.       {
  43.          resultIntersections[0] = resultIntersections[1];
  44.          resultIntersections.pop();
  45.       }
  46.       var i = 0;
  47.       while(i < resultIntersections.length)
  48.       {
  49.          intersectionPoints[i] = new Object();
  50.          intersectionPoints[i].x = rkRay.origin.x + resultIntersections[i] * rkRay.direction.x;
  51.          intersectionPoints[i].y = rkRay.origin.y + resultIntersections[i] * rkRay.direction.y;
  52.          i++;
  53.       }
  54.    }
  55.    return intersectionPoints;
  56. }
  57. function ArcContainsPoint(rkArc, P)
  58. {
  59.    var angleToP = Math.atan2(P.y - rkArc.center.y,P.x - rkArc.center.x) / 3.141592653589793 * 180;
  60.    var contains = Math.abs(angleToP - rkArc.angularMidpoint) <= rkArc.arcLength * 0.5;
  61.    if(!contains)
  62.    {
  63.       angleToP = Wrap(angleToP,0,360);
  64.       var angularMidpoint = Wrap(rkArc.angularMidpoint,0,360);
  65.       return Math.abs(angleToP - angularMidpoint) <= rkArc.arcLength * 0.5;
  66.    }
  67.    return contains;
  68. }
  69. function FindRayArcIntersections(rkRay, rkArc)
  70. {
  71.    var resultIntersections = FindLineIntersections(rkRay.origin,rkRay.direction,rkArc.center,rkArc.radius);
  72.    var intersectionPoints = new Array();
  73.    if(resultIntersections.length)
  74.    {
  75.       if(resultIntersections.length == 1)
  76.       {
  77.          if(resultIntersections[0] < 0)
  78.          {
  79.             resultIntersections.splice(0);
  80.          }
  81.       }
  82.       else if(resultIntersections[1] < 0)
  83.       {
  84.          resultIntersections.splice(0);
  85.       }
  86.       else if(resultIntersections[0] < 0)
  87.       {
  88.          resultIntersections[0] = resultIntersections[1];
  89.          resultIntersections.pop();
  90.       }
  91.       var iResultPoints = 0;
  92.       var i = 0;
  93.       while(i < resultIntersections.length)
  94.       {
  95.          intersectionPoints[iResultPoints] = new Object();
  96.          intersectionPoints[iResultPoints].x = rkRay.origin.x + resultIntersections[i] * rkRay.direction.x;
  97.          intersectionPoints[iResultPoints].y = rkRay.origin.y + resultIntersections[i] * rkRay.direction.y;
  98.          if(ArcContainsPoint(rkArc,intersectionPoints[iResultPoints]))
  99.          {
  100.             iResultPoints++;
  101.          }
  102.          else
  103.          {
  104.             intersectionPoints[iResultPoints] = null;
  105.          }
  106.          i++;
  107.       }
  108.    }
  109.    return intersectionPoints;
  110. }
  111. function FindSegmentArcIntersections(rkSegment, rkArc)
  112. {
  113.    var resultIntersections = FindLineIntersections(rkSegment.origin,rkSegment.direction,rkArc.center,rkArc.radius);
  114.    var intersectionInfo = null;
  115.    if(resultIntersections.length)
  116.    {
  117.       if(resultIntersections.length == 1)
  118.       {
  119.          if(resultIntersections[0] < 0 || resultIntersections[0] > 1)
  120.          {
  121.          }
  122.       }
  123.       else if(resultIntersections[1] < 0 || resultIntersections[0] > 1)
  124.       {
  125.          resultIntersections.splice(0);
  126.       }
  127.       else if(resultIntersections[1] <= 1)
  128.       {
  129.          if(resultIntersections[0] < 0)
  130.          {
  131.             resultIntersections[0] = resultIntersections[1];
  132.             resultIntersections.pop();
  133.          }
  134.       }
  135.       else if(resultIntersections[0] >= 0)
  136.       {
  137.          resultIntersections.pop();
  138.       }
  139.       else
  140.       {
  141.          resultIntersections.splice(0);
  142.       }
  143.       var i = 0;
  144.       while(i < resultIntersections.length)
  145.       {
  146.          intersectionInfo = new Object();
  147.          intersectionInfo.touchTime = resultIntersections[i] * 0.9;
  148.          intersectionInfo.touchPoint = new Object();
  149.          intersectionInfo.touchPoint.x = rkSegment.origin.x + resultIntersections[i] * rkSegment.direction.x * 0.9;
  150.          intersectionInfo.touchPoint.y = rkSegment.origin.y + resultIntersections[i] * rkSegment.direction.y * 0.9;
  151.          if(ArcContainsPoint(rkArc,intersectionInfo.touchPoint))
  152.          {
  153.             break;
  154.          }
  155.          intersectionInfo = null;
  156.          i++;
  157.       }
  158.    }
  159.    return intersectionInfo;
  160. }
  161. function FindSegmentCircleIntersections(rkSegment, rkCircle)
  162. {
  163.    var resultIntersections = FindLineIntersections(rkSegment.origin,rkSegment.direction,rkCircle.center,rkCircle.radius);
  164.    var intersectionInfo = null;
  165.    if(resultIntersections.length)
  166.    {
  167.       if(resultIntersections.length == 1)
  168.       {
  169.          if(resultIntersections[0] < 0 || resultIntersections[0] > 1)
  170.          {
  171.          }
  172.       }
  173.       else if(resultIntersections[1] < 0 || resultIntersections[0] > 1)
  174.       {
  175.          resultIntersections.splice(0);
  176.       }
  177.       else if(resultIntersections[1] <= 1)
  178.       {
  179.          if(resultIntersections[0] < 0)
  180.          {
  181.             resultIntersections[0] = resultIntersections[1];
  182.             resultIntersections.pop();
  183.          }
  184.       }
  185.       else if(resultIntersections[0] >= 0)
  186.       {
  187.          resultIntersections.pop();
  188.       }
  189.       else
  190.       {
  191.          resultIntersections.splice(0);
  192.       }
  193.       var i = 0;
  194.       while(i < resultIntersections.length)
  195.       {
  196.          intersectionInfo = new Object();
  197.          intersectionInfo.touchTime = resultIntersections[i] * 0.9;
  198.          intersectionInfo.touchPoint = new Object();
  199.          intersectionInfo.touchPoint.x = rkSegment.origin.x + resultIntersections[i] * rkSegment.direction.x * 0.9;
  200.          intersectionInfo.touchPoint.y = rkSegment.origin.y + resultIntersections[i] * rkSegment.direction.y * 0.9;
  201.          i++;
  202.       }
  203.    }
  204.    return intersectionInfo;
  205. }
  206. function CPaddle()
  207. {
  208.    this._score = 0;
  209.    if(this._isAIControlled)
  210.    {
  211.       this.onEnterFrame = this.PerformAI;
  212.    }
  213.    else
  214.    {
  215.       this.onEnterFrame = this.UpdateRotation;
  216.       this._rotationTarget = new Object();
  217.    }
  218.    this._englishAngle = 0;
  219. }
  220. function IsRightOfHumpLine(angle, buffer)
  221. {
  222.    if(angle + buffer > 120 && angle - buffer < 150)
  223.    {
  224.       return undefined;
  225.    }
  226.    return angle + buffer <= 120 && angle - buffer > -60;
  227. }
  228. function IsLeftOfHumpLine(angle, buffer)
  229. {
  230.    if(angle + buffer > 120 && angle - buffer < 150)
  231.    {
  232.       return undefined;
  233.    }
  234.    return angle - buffer >= 150 || angle + buffer < -30;
  235. }
  236. CPaddle.prototype = new MovieClip();
  237. Object.registerClass("CPaddle",CPaddle);
  238. CPaddle.prototype.Score = function()
  239. {
  240.    this._score = this._score + 1;
  241. };
  242. CPaddle.prototype.ScoreFailure = function()
  243. {
  244.    this._score--;
  245. };
  246. CPaddle.prototype.UpdateRotation = function()
  247. {
  248.    if(this._rotationTarget)
  249.    {
  250.       var desiredRotation = this._rotationTarget + this._englishAngle;
  251.       var currentRotation = WrapAngle(this._rotation + 45);
  252.       desiredRotation = WrapAngle(desiredRotation + 45);
  253.       var deltaRotation = desiredRotation - currentRotation;
  254.       var deltaRotationSign = deltaRotation >= 0 ? 1 : -1;
  255.       currentRotation += deltaRotationSign * Math.min(Math.abs(deltaRotation),this._maxRotationSpeed);
  256.       this._rotation = currentRotation - 45;
  257.    }
  258.    this.ClampPaddleRotation();
  259. };
  260. CPaddle.prototype.ClampPaddleRotation = function()
  261. {
  262.    var deltaRotation = WrapAngle(this._rotation - this._oldRotation);
  263.    if(deltaRotation >= 0 && IsRightOfHumpLine(this._oldRotation,this._arcLength * 0.5) && !IsRightOfHumpLine(this._rotation,this._arcLength * 0.5))
  264.    {
  265.       this._rotation = Math.min(110,Wrap(this._rotation,0,360));
  266.    }
  267.    else if(deltaRotation <= 0 && IsLeftOfHumpLine(this._oldRotation,this._arcLength * 0.5) && !IsLeftOfHumpLine(this._rotation,this._arcLength * 0.5))
  268.    {
  269.       this._rotation = Math.max(160,this._rotation);
  270.    }
  271.    this._oldRotation = this._rotation;
  272. };
  273. CPaddle.prototype.PerformAI = function()
  274. {
  275.    if(this._isReceiving || !this._rotationTarget)
  276.    {
  277.       var velNormal = new Object();
  278.       velNormal.x = this._parent.puck1._velX;
  279.       velNormal.y = this._parent.puck1._velY;
  280.       var vecMagnitude = Math.sqrt(velNormal.x * velNormal.x + velNormal.y * velNormal.y);
  281.       velNormal.x /= vecMagnitude;
  282.       velNormal.y /= vecMagnitude;
  283.       var intersectionPoints = FindRayCircleIntersections({origin:{x:this._parent.puck1._x,y:this._parent.puck1._y},direction:{x:velNormal.x,y:velNormal.y}},{center:{x:0,y:0},radius:122});
  284.       if(intersectionPoints.length > 0)
  285.       {
  286.          this._rotationTarget = Math.atan2(intersectionPoints[0].y,intersectionPoints[0].x) / 3.141592653589793 * 180;
  287.       }
  288.       if(this._rotationTarget && !this._isReceiving)
  289.       {
  290.          var distFromRotationTarget = Math.abs(this._rotationTarget - this._rotation);
  291.          if(distFromRotationTarget < this._arcLength * 2)
  292.          {
  293.             var randomDirection = Math.random() >= 0.5 ? 1 : -1;
  294.             var avoidanceTarget = this._rotationTarget + randomDirection * this._arcLength * 2;
  295.             if(IsRightOfHumpLine(avoidanceTarget) && !IsRightOfHumpLine(this._rotationTarget) || IsLeftOfHumpLine(avoidanceTarget) && !IsLeftOfHumpLine(this._rotationTarget))
  296.             {
  297.                avoidanceTarget = this._rotationTarget + (- randomDirection) * this._arcLength * 2;
  298.             }
  299.             this._rotationTarget = avoidanceTarget;
  300.          }
  301.          else
  302.          {
  303.             this._rotationTarget = null;
  304.          }
  305.       }
  306.    }
  307.    this.UpdateRotation();
  308. };
  309. CPaddle.prototype.BeginReceiving = function()
  310. {
  311.    this._isReceiving = true;
  312.    if(this._isAIControlled)
  313.    {
  314.       this._englishAngle = Math.random() * 10 - 5;
  315.       this._rotationTarget = null;
  316.    }
  317.    else
  318.    {
  319.       this._englishAngle = 0;
  320.    }
  321. };
  322. CPaddle.prototype.FinishedReceiving = function()
  323. {
  324.    this._isReceiving = false;
  325.    this._rotationTarget = null;
  326. };
  327.